package getfour.gui;

import getfour.Static;
import getfour.game.IMove;
import getfour.game.Logic;
import getfour.game.Move;
import getfour.game.Player;
import getfour.game.Replay;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Observable;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JToggleButton;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;
import javax.swing.border.LineBorder;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class History extends javax.swing.JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 5283401799307466214L;
	private final String pathN = "pictures/free.png";
	private final String pathO = "pictures/o2.png";
	private final String pathX = "pictures/x2.png";
	
	{
		//Set Look & Feel
		try {
			javax.swing.UIManager.setLookAndFeel("com.jgoodies.looks.plastic.Plastic3DLookAndFeel");
		} catch(Exception e) {
			e.printStackTrace();
		}
	}

	private final int ROWS;
	private final int COLUMNS;
	
	private JPanel pnlMain;
	private JLabel lblDescEnemyName;
	private JPanel pnlRightSide;
	private JComboBox cmbGame;
	private JTextField jTextField3;
	private JLabel lblPlayerO;
	private JLabel lblPlayerX;
	private JPanel pnlGame;
	private JPanel pnlNavi;
	private JPanel pnlLeftSide;
	private JComboBox cmbMatch;
	private JButton btnForward;
	private JButton btnEnd;
	private JButton btnBackward;
	private JButton btnBeginning;
	private JToggleButton jToggleButton1;
	private JLabel jLabel2;
	private JTextField jTextField2;
	private JTextField jTextField1;
	private JLabel lblEnemyName;
	private ImagePanel[][] pnlGameFields;
	private Logic gameLogic;
	private Replay currentReplay;
	private int currentMove = -1;
	
	public History(Logic game) {
		super();
		
		gameLogic = game;
		ROWS = gameLogic.getVariable("system.game.rows").getValueInt();
		COLUMNS = gameLogic.getVariable("system.game.columns").getValueInt();
		pnlGameFields = new ImagePanel[ROWS][COLUMNS];
		
		initGUI();
		resetBoard();
		
		for (int i = 1; i <= gameLogic.getGames(); i++)
			cmbGame.addItem(i);
		
		//for (int i = 1; i <= gameLogic.getMatches(1); i++)
		//	cmbMatch.addItem(i);
	}
	
	private void resetBoard() {
		for (int i = ROWS -1; i >= 0; i--) {
			for (int j = 0; j < COLUMNS; j++) {
				pnlGameFields[i][j].setImage(pathN);
			}
		}
		
		refreshCurrentPlayer(false);
	}
	
	private void initGUI() {
		try {
			{
				this.setTitle("Vier Gewinnt Agent");
				this.setPreferredSize(new java.awt.Dimension(640, 480));
				this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
				setDefaultLookAndFeelDecorated(false);
			}
			{
				pnlMain = new JPanel();
				BoxLayout pnlMainLayout = new BoxLayout(pnlMain, javax.swing.BoxLayout.X_AXIS);
				getContentPane().add(pnlMain, BorderLayout.CENTER);
				pnlMain.setPreferredSize(new java.awt.Dimension(640, 480));
				pnlMain.setLayout(pnlMainLayout);
				pnlMain.setMaximumSize(new java.awt.Dimension(640, 480));
				pnlMain.setMinimumSize(new java.awt.Dimension(640, 480));
				{
					pnlLeftSide = new JPanel();
					pnlMain.add(pnlLeftSide);
					pnlLeftSide.setPreferredSize(new java.awt.Dimension(111, 453));
					pnlLeftSide.setLayout(null);
					pnlLeftSide.setSize(163, 453);
					{
						jTextField1 = new JTextField();
						pnlLeftSide.add(jTextField1);
						jTextField1.setText("Satznummer:");
						jTextField1.setBounds(18, 180, 96, 22);
						jTextField1.setBackground(new java.awt.Color(212,208,200));
						jTextField1.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
						jTextField1.setFont(new java.awt.Font("Tahoma",1,11));
					}
					{
						jTextField2 = new JTextField();
						pnlLeftSide.add(jTextField2);
						jTextField2.setFont(new java.awt.Font("Tahoma",1,11));
						jTextField2.setText("Spielstand:");
						jTextField2.setBackground(new java.awt.Color(212,208,200));
						jTextField2.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
						jTextField2.setBounds(18, 254, 96, 22);
					}
					{
						jLabel2 = new JLabel();
						pnlLeftSide.add(jLabel2);
						jLabel2.setText("0 : 0");
						jLabel2.setHorizontalAlignment(SwingConstants.CENTER);
						jLabel2.setHorizontalTextPosition(SwingConstants.CENTER);
						jLabel2.setBackground(new java.awt.Color(255,255,255));
						jLabel2.setFont(new java.awt.Font("Tahoma",1,11));
						jLabel2.setOpaque(true);
						jLabel2.setBorder(new LineBorder(new java.awt.Color(0,0,0),1,false));
						jLabel2.setBounds(12, 276, 119, 24);
					}
					{
						jToggleButton1 = new JToggleButton();
						pnlLeftSide.add(jToggleButton1);
						jToggleButton1.setText("Beenden");
						jToggleButton1.setBounds(12, 394, 120, 48);
						jToggleButton1.setFont(new java.awt.Font("Tahoma",1,11));
						jToggleButton1.addActionListener(new ActionListener() {
							public void actionPerformed(ActionEvent evt) {
								setVisible(false);
								dispose();
							}
						});
					}
					{
						ComboBoxModel cmbMatchModel = 
							new DefaultComboBoxModel(
									new String[] { });
						cmbMatch = new JComboBox();
						pnlLeftSide.add(cmbMatch);
						cmbMatch.setModel(cmbMatchModel);
						cmbMatch.setBounds(12, 202, 119, 30);
						cmbMatch.setFont(new java.awt.Font("Tahoma",1,11));
						cmbMatch.setBackground(new java.awt.Color(255,255,255));
						cmbMatch.addActionListener(new ActionListener() {
							public void actionPerformed(ActionEvent evt) {
								if (cmbMatch.getSelectedIndex() >= 0) {
									currentReplay = gameLogic.getReplay(cmbGame.getSelectedIndex() +1, cmbMatch.getSelectedIndex() +1);
									lblEnemyName.setText(currentReplay.getEnemyName());
									refreshCurrentPlayer(false);
								}
							}
						});
					}
					{
						lblPlayerX = new JLabel();
						pnlLeftSide.add(lblPlayerX);
						lblPlayerX.setText("Spieler X");
						lblPlayerX.setBounds(18, 16, 139, 22);
						lblPlayerX.setFont(new java.awt.Font("Tahoma",0,11));
						lblPlayerX.setForeground(new java.awt.Color(223,0,0));
					}
					{
						lblPlayerO = new JLabel();
						pnlLeftSide.add(lblPlayerO);
						lblPlayerO.setText("Spieler O");
						lblPlayerO.setFont(new java.awt.Font("Tahoma",0,11));
						lblPlayerO.setForeground(new java.awt.Color(0,128,0));
						lblPlayerO.setBounds(18, 50, 139, 22);
					}
					{
						jTextField3 = new JTextField();
						pnlLeftSide.add(jTextField3);
						jTextField3.setFont(new java.awt.Font("Tahoma",1,11));
						jTextField3.setText("Spielnummer");
						jTextField3.setBackground(new java.awt.Color(212,208,200));
						jTextField3.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
						jTextField3.setBounds(18, 116, 96, 22);
					}
					{
						ComboBoxModel jComboBox1Model = 
							new DefaultComboBoxModel(
									new String[] { });
						cmbGame = new JComboBox();
						pnlLeftSide.add(cmbGame);
						cmbGame.setModel(jComboBox1Model);
						cmbGame.setBackground(new java.awt.Color(255,255,255));
						cmbGame.setFont(new java.awt.Font("Tahoma",1,11));
						cmbGame.setBounds(12, 138, 119, 30);
						cmbGame.addActionListener(new ActionListener() {
							public void actionPerformed(ActionEvent evt) {
								cmbMatch.removeAllItems();
								
								for (int i = 1; i <= gameLogic.getMatches(cmbGame.getSelectedIndex() +1); i++)
									cmbMatch.addItem(i);
								
								currentReplay = gameLogic.getReplay(cmbGame.getSelectedIndex() +1, 1);
								lblEnemyName.setText(currentReplay.getEnemyName());
								refreshCurrentPlayer(false);
							}
						});
					}
				}
				{
					pnlRightSide = new JPanel();
					BoxLayout pnlRightSideLayout = new BoxLayout(pnlRightSide, javax.swing.BoxLayout.Y_AXIS);
					pnlRightSide.setLayout(pnlRightSideLayout);
					pnlMain.add(pnlRightSide);
					pnlRightSide.setPreferredSize(new java.awt.Dimension(457, 480));
					{
						pnlNavi = new JPanel();
						pnlRightSide.add(pnlNavi);
						pnlNavi.setPreferredSize(new java.awt.Dimension(453, 49));
						pnlNavi.setLayout(null);
						{
							lblDescEnemyName = new JLabel();
							pnlNavi.add(lblDescEnemyName);
							lblDescEnemyName.setText("Name des Gegners:");
							lblDescEnemyName.setBounds(2, 6, 104, 27);
							lblDescEnemyName.setHorizontalAlignment(SwingConstants.CENTER);
							lblDescEnemyName.setHorizontalTextPosition(SwingConstants.CENTER);
						}
						{
							btnBeginning = new JButton();
							pnlNavi.add(btnBeginning);
							btnBeginning.setText("|<");
							btnBeginning.setBorder(new LineBorder(new java.awt.Color(0,0,0), 1, false));
							btnBeginning.setBounds(346, 5, 28, 28);
							btnBeginning.addActionListener(new ActionListener() {
								public void actionPerformed(ActionEvent evt) {
									currentMove = -1;
									resetBoard();
								}
							});
						}
						{
							btnBackward = new JButton();
							pnlNavi.add(btnBackward);
							btnBackward.setText("<");
							btnBackward.setBorder(new LineBorder(new java.awt.Color(0,0,0), 1, false));
							btnBackward.setBounds(380, 5, 28, 28);
							btnBackward.addActionListener(new ActionListener() {
								public void actionPerformed(ActionEvent evt) {							
									if (currentMove >= 0) {
										displayMove(currentReplay.getMoves().get(currentMove), true);
										currentMove--;
									}
								}
							});
						}
						{
							btnForward = new JButton();
							pnlNavi.add(btnForward);
							btnForward.setText(">");
							btnForward.setBounds(414, 5, 28, 28);
							btnForward.setBorder(new LineBorder(new java.awt.Color(0,0,0), 1, false));
							btnForward.addActionListener(new ActionListener() {
								public void actionPerformed(ActionEvent evt) {
									if (currentMove +1 < currentReplay.getMoves().size()) {
										currentMove++;
										displayMove(currentReplay.getMoves().get(currentMove), false);
									}
								}
							});
						}
						{
							btnEnd = new JButton();
							pnlNavi.add(btnEnd);
							btnEnd.setText(">|");
							btnEnd.setBorder(new LineBorder(new java.awt.Color(0,0,0),1,false));
							btnEnd.setBounds(448, 5, 28, 28);
							btnEnd.addActionListener(new ActionListener() {
								public void actionPerformed(ActionEvent evt) {
									for (int i = (currentMove > -1 ? currentMove : 0); i < currentReplay.getMoves().size(); i++) {
										currentMove = i;
										displayMove(currentReplay.getMoves().get(i), false);
									}
								}
							});
						}
						{
							lblEnemyName = new JLabel();
							pnlNavi.add(lblEnemyName);
							lblEnemyName.setText("Anonymous Player");
							lblEnemyName.setBounds(105, 12, 233, 14);
						}
					}
					{
						pnlGame = new JPanel();
						pnlRightSide.add(pnlGame);
						GridLayout pnlGameLayout = new GridLayout(ROWS, COLUMNS);
						pnlGameLayout.setHgap(0);
						pnlGameLayout.setVgap(0);
						pnlGame.setLayout(pnlGameLayout);
						pnlGame.setPreferredSize(new java.awt.Dimension(556, 418));
						pnlGame.setSize(482, 418);
						pnlGame.setMaximumSize(new java.awt.Dimension(482, 418));
						pnlGame.setMinimumSize(new java.awt.Dimension(482, 418));
					}
					
					{
						for (int i = ROWS -1; i >= 0; i--) {
							for (int j = 0; j < COLUMNS; j++) {
								pnlGameFields[i][j] = new ImagePanel();
								pnlGame.add(pnlGameFields[i][j]);
								pnlGameFields[i][j].setBorder(new LineBorder(new java.awt.Color(0,0,0), 0, false));
							}
						}
					}
				}
			}
			pack();
			this.setSize(640, 480);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void displayMove(Move move, boolean reverse) {
		if (move.getRow() >= 0 && move.getColumn() >= 0) {
			ImagePanel tmp = pnlGameFields[move.getRow()][move.getColumn()];
			
			if (reverse) tmp.setImage(pathN);
			else if (move.getPlayer().equals(Player.X)) {
		   		 tmp.setImage(pathX);
			}
		   	else {
		   		 tmp.setImage(pathO);
		   		 
		   	}
			
			refreshCurrentPlayer(reverse);
		}
		else {
			Static.Error("Invalid move.");
		}
	}
	
	private void refreshCurrentPlayer(Boolean reverse) {
		if (currentReplay != null && currentMove + 1 < currentReplay.getMoves().size()) {
			Move move = currentReplay.getMoves().get(reverse ? currentMove : currentMove +1);
				
			// Anzeige mit letztem Zug aktualisieren
			if (move.getPlayer() == Player.O) {
				lblPlayerX.setText("Spieler X");
				lblPlayerO.setText("Spieler O (am Zug)");
			}
			else {
				lblPlayerX.setText("Spieler X (am Zug)");
				lblPlayerO.setText("Spieler O");
			}
		}
		else {
			lblPlayerX.setText("Spieler X");
			lblPlayerO.setText("Spieler O");
		}
	}

}
